<?

abstract class Core {

	public $db;
	public $sections;
	public $defaults = array();
	public $options = array();
	
	private $_error = '';
	private $_debug = array();
	private $_db_debug = false;

	function __construct () {
		// Create database connection
		$this->db = new ez_sql(EZSQL_DB_USER, EZSQL_DB_PASSWORD, EZSQL_DB_NAME, EZSQL_DB_HOST);
		// Start session
		$this->session = new Session();
		// Instancio la clase load
		$this->load = Load::getInstance();
		// Seteo las secciones para que esten disponibles globalmente
		$this->sections = json_decode(CONFIG_SECTIONS, true);
	}
	
	public function sections () {
		return json_decode(CONFIG_SECTIONS, true);
	}

	/**
	 * Set options
	 * 
	 * @param array $default Array Default options for the caller class
	 * @param array $options (optional) Valid options for this class. If empty, performs as a getter.
	 * @return array $options List of options
	 */
	/* 
	public function options ($default = null, $options = null) {
		if (!$default)
			return false;
		if ($options && is_array($options)) {
			foreach ($options as $k => $v)
				$default[$k] = $v;
		}
		$this->options = $default;
		return $default;
	}
	*/
	
	public function options ($options = null) {
		if (!empty($options) && is_array($options)) {
			$defaults = $this->defaults;
			foreach ($options as $k => $v) {
				if (array_key_exists($k, $defaults)) {
					$this->options[$k] = $v;
				}
			}
		}
		return $this->options;
	}
	
	

	function str_to_url($str) {
		$arr_to_replace = array (
			' '		=> '-',
			', '	=> '-',
			'/'		=> '-',
			','		=> ' '
		);

		return urlencode(str_replace(array_keys($arr_to_replace), array_values($arr_to_replace), $str));
	}

	/**
	 * Set the debug for any query
	 * 
	 * @param boolean $debug Turn on/off debug
	 * @return boolean
	 */
	public function db_debug ($debug) {
		return $this->db->debug = $debug;
	}

	/**
	 * Set or return the error message
	 * 
	 * @param string $msg The message
	 * @return string The error
	 */
	function error($msg=null) {
		if ($msg)
			$this->_error = $msg;
		return $this->_error;
	}

	/*
	* Descripcion: Setea o devuelve informacion de debug
	* Params: String
	* Return: String
	*/
	function debug($set=false) {
		if ($set) {
			// Obtengo la clase y metodo que me llamo
			$debug = debug_backtrace(true);
			$caller = $debug[1];
			$file = end($debug);
			// Almaceno la informacion de debug
			$this->_debug = array(
				'Date' => '['.date("Y-m-d H:i:s").']',
				'Class' => $caller['file'].': '.$caller['line'],
				'File' => $file['file'].': '.$file['line'],
				'Object' => $file['class'].'->'.$file['function'].'('.implode(',', $file['args']).')',
				'Last Error' => $this->error()
			);
		}
		return $this->_debug;
	}

	/*
	* Descripcion: Setea un mensaje de error y devuelve false
	* Params: int, int, tinyint
	* Return: (bool)
	*/
	protected function fail ($msg=null) {
		// Almaceno el mensaje de error
		$msg = ($msg)? $msg : 'Unknown error';
		// Seteo el error
		$this->error($msg);
		// Seteo la informacion de debug
		$this->debug(true);

		return false;
	}

	public function printer($el) {
		echo '<pre>'.print_r($el, true).'</pre>';
	}

}

?>
